package org.autumn.algorithm.sort;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class SortManager {

    private SortManager() {
        //prevent instantiate
    }

    private static Map<Class<? extends Sort>, Sort> sortsMap = new HashMap<>();

    static {
        sortsMap.put(SelectionSort.class, new SelectionSort());
        sortsMap.put(BubbleSort.class, new BubbleSort());
        sortsMap.put(InsertSort.class, new InsertSort());
    }

    /**
     *  排序
     *  默认按照选择排序
     */
    public static void sort(int[] arr) {
        sort(arr, SelectionSort.class);
    }

    /**
     * 排序
     * 指定排序算法,如果不存在,默认选择排序
     */
    public static void sort(int[] arr, Class<? extends Sort> clazz) {
        Sort sort = Optional.ofNullable(sortsMap.get(clazz)).orElseGet(() -> {
            return sortsMap.get(SelectionSort.class);
        });
        sort.sort(arr);
    }
}
